package com.javabase.y2022.m04.day0419;

import java.util.Stack;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/4/19 下午 04:10
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        String s = "loveleetcode";
        char c = 'e';
        Solution solution = new Solution();
        int[] answer = solution.shortestToChar(s, c);
        for (int i = 0; i < answer.length; i++) {
            System.out.print("  " + answer[i]);
        }
    }
    public int[] shortestToChar(String s, char c) {
        int n = s.length();
        int[] ans = new int[s.length()];
        for (int i = 0, index = -n; i < n; i++) {
            if(s.charAt(i) == c){
                index = i;
            }
            ans[i] = i - index;
        }

        for(int i = n-1, index = 2*n; i >=0; i--){
            if(s.charAt(i) == c){
                index = i;
            }
            ans[i] = Math.min(index - i, ans[i]);
        }
        return ans;
    }
}
